﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CYTS.Aoyou.Framework.Service;
using CYTS.Aoyou.Order.ServiceInterface;
using CYTS.Aoyou.Order;
using CYTS.Aoyou.Order;
using CYTS.Aoyou.Framework.Session;

namespace CYTS.Aoyou.Order.Website.Controllers
{
    public class IndexController : Controller
    {
        //
        // GET: /Index/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Index/Details/5

        public ActionResult Details(string id)
        {
            Website.Models.OrderInfo orderInfoView = null;
            orderInfoView = SessionHelper.GetSession<Website.Models.OrderInfo>(id);
            if (orderInfoView == null)
            {
                IOrderService service = ServiceFactory.GetInstance<IOrderService>();
                ServiceEntity.OrderInfo orderInfo = service.GetOrderInfo(Convert.ToInt32(id));
                if (orderInfo != null)
                {
                    orderInfoView = new Models.OrderInfo();
                    orderInfoView.OrderID = orderInfo.OrderID;
                    orderInfoView.OrderMoney = orderInfo.OrderMoney;
                    orderInfoView.PaidMoney = orderInfo.PaidMoney;
                    orderInfoView.ProductFullName = orderInfo.ProductName;
                    orderInfoView.UnpaidMoney = orderInfo.UnpaidMoney;
                    if (orderInfo.ProductName.Contains('【') && orderInfo.ProductName.Contains('】'))
                    {
                        orderInfoView.ProductCategory = orderInfo.ProductName.Substring(orderInfo.ProductName.IndexOf('【'), orderInfo.ProductName.IndexOf('】') + 1);
                    }

                    IOrderMsmqService msmqService = ServiceFactory.GetInstance<IOrderMsmqService>();
                    msmqService.HandleOrder(orderInfo);
                }

                SessionHelper.SetSession(id, orderInfoView);

            }

            return View(orderInfoView);
        }

        //
        // GET: /Index/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Index/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Index/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Index/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Index/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Index/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
